package com.godGuang.message.config;

import com.alibaba.fastjson2.JSON;
import com.godGuang.common.security.service.TokenService;
import com.godGuang.common.system.domain.SysUser;
import com.godGuang.message.domin.Account;
import com.godGuang.message.utils.MailUtils;
import com.rabbitmq.client.Channel;
import lombok.extern.log4j.Log4j2;
import org.apache.el.parser.Token;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * @Author: 李雪仁
 * @Description: 专高四
 */


/**
 * 短信配置
 */
@Component
@Log4j2
public class SmsConfig {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private TokenService tokenService;

    private final String ADD_LOG_QUEUE_NAME = "aaa";

    /**
     * 监听消息队列
     * =================================================================================================================
     */
    @RabbitListener(queuesToDeclare = {@Queue(ADD_LOG_QUEUE_NAME)})
    public void sendSmsConfig(String tel, Message message, Channel channel) {
        log.info("监听消息队列，消息内容: {}，开始监听...", tel);
        // 获取消息队列ID
        String messageId = message.getMessageProperties().getMessageId();

        Long smsKey = redisTemplate.opsForSet().add(ADD_LOG_QUEUE_NAME, messageId);
        if (smsKey != null && smsKey == 1) {
            try {

                // 创建一个MailUtils实例
               MailUtils mailUtils = new MailUtils();
               Account account = JSON.parseObject(tel, Account.class);
                String accountTitle = account.getAccountTitle();
                String accountDefault = account.getAccountDefault();
                Date accountTime = account.getAccountTime();
                String content = accountTime + accountTitle + accountDefault;
                SysUser sysUser = tokenService.getLoginUser().getSysUser();
                String email = sysUser.getEmail();

                // 发送简单的邮件
                // 参数1: 收件人邮箱地址
                // 参数2: 邮件标题
                // 参数3: 邮件内容
                mailUtils.sendSimpleEmail(email, accountTitle, content);



                // 确认消息消费成功
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                log.info("监听消息队列，消息内容: {}，监听成功...", tel);
            } catch (Exception e) {
                redisTemplate.opsForSet().remove(ADD_LOG_QUEUE_NAME, messageId);
                log.error("消息内容:", e);
            }
        } else {
            log.info("监听消息队列，消息内容: {}，监听失败...", tel);
            try {
                // 拒绝消息
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
            } catch (IOException ex) {
                // 处理IO异常
                log.error("消息拒绝失败", ex);
            }
        }
    }
}
